<template>
  <div id="userManagePage">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="输入账号" allow-clear />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.userName" placeholder="输入用户名" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px" />
    <!-- 用户管理卡片列表 -->
    <div class="user-management-list">
      <div 
        v-for="record in dataList" 
        :key="record.id" 
        class="user-card"
      >
        <!-- 用户头像区域 -->
        <div class="user-avatar">
          <a-image 
            :src="record.userAvatar || '/default-avatar.png'" 
            :width="120" 
            :height="120"
            class="avatar-image"
            :preview="{ mask: '预览' }"
          />
          <div class="user-overlay">
            <div class="user-id">ID: {{ record.id }}</div>
            <div class="user-tags">
              <a-tag :color="record.userRole === 'admin' ? 'green' : 'blue'">
                {{ record.userRole === 'admin' ? '管理员' : '普通用户' }}
              </a-tag>
              <a-tag :color="(record.isBanned ?? 0) === 0 ? 'green' : 'red'">
                {{ (record.isBanned ?? 0) === 0 ? '正常' : '已封禁' }}
              </a-tag>
            </div>
          </div>
        </div>
        
        <!-- 用户信息区域 -->
        <div class="user-info">
          <!-- 基本信息 -->
          <div class="info-section">
            <h3 class="user-name" :title="record.userName">{{ record.userName || '未设置用户名' }}</h3>
            <p class="user-account" :title="record.userAccount">账号: {{ record.userAccount }}</p>
          </div>
          
          <!-- 详细信息 -->
          <div class="info-details">
            <div class="info-row">
              <div class="info-item full-width">
                <span class="info-label">个人简介:</span>
                <span class="info-value" :title="record.userProfile">
                  {{ record.userProfile || '暂无简介' }}
                </span>
              </div>
            </div>
            
            <!-- 时间信息 -->
            <div class="info-row">
              <div class="info-item">
                <span class="info-label">创建时间:</span>
                <span class="info-value">{{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm') }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 操作按钮区域 -->
        <div class="user-actions">
          <a-space>
            <a-button 
              v-if="(record.isBanned ?? 0) === 0"
              type="primary"
              danger
              size="small"
              @click="doBanUser(record)"
              :icon="h(StopOutlined)"
            >
              封禁用户
            </a-button>
            <a-button 
              v-if="record.isBanned === 1"
              type="primary"
              size="small"
              @click="doUnbanUser(record)"
              :icon="h(CheckOutlined)"
            >
              解封用户
            </a-button>
            <a-button 
              danger 
              size="small"
              @click="doDelete(record.id)"
              :icon="h(DeleteOutlined)"
            >
              删除用户
            </a-button>
          </a-space>
        </div>
      </div>
    </div>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <a-pagination
        v-model:current="searchParams.current"
        v-model:pageSize="searchParams.pageSize"
        :total="total"
        :show-size-changer="true"
        :show-quick-jumper="true"
        :show-total="(total, range) => `共 ${total} 条记录，第 ${range[0]}-${range[1]} 条`"
        @change="onPageChange"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref, h } from 'vue'
import { deleteUserUsingPost, listUserVoByPageUsingPost, userBannedUsingPost, userUnBannedUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import { StopOutlined, CheckOutlined, DeleteOutlined } from '@ant-design/icons-vue'
import dayjs from 'dayjs'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 定义数据
const dataList = ref<API.UserVO[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'ascend',
})

// 获取数据
const fetchData = async () => {
  const res = await listUserVoByPageUsingPost({
    ...searchParams,
  })
  if (res.data.code === 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}

// 页面加载时获取数据，请求一次
onMounted(() => {
  fetchData()
})

// 分页变化处理
const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

// 搜索数据
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) {
    return
  }
  const res = await deleteUserUsingPost({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
    // 刷新数据
    fetchData()
  } else {
    message.error('删除失败')
  }
}

const doBanUser = async (user: API.UserVO) => {
  if (!user.userAccount) {
    message.error('用户账号不能为空')
    return
  }
  
  try {
    const res = await userBannedUsingPost([user.userAccount])
    
    if (res.data.code === 0) {
      message.success('用户封禁成功')
      fetchData()
    } else {
      message.error('封禁失败：' + res.data.message)
    }
  } catch (error) {
    console.error('封禁用户失败:', error)
    message.error('封禁用户失败')
  }
}

const doUnbanUser = async (user: API.UserVO) => {
  if (!user.userAccount) {
    message.error('用户账号不能为空')
    return
  }
  
  try {
    const res = await userUnBannedUsingPost([user.userAccount])
    
    if (res.data.code === 0) {
      message.success('用户解封成功')
      fetchData()
    } else {
      message.error('解封失败：' + res.data.message)
    }
  } catch (error) {
    console.error('解封用户失败:', error)
    message.error('解封用户失败')
  }
}
</script>

<style scoped>
#userManagePage {
  padding: 2rem;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  margin: 1rem;
}

/* 黑色模式下的页面样式 */
[data-theme="dark"] #userManagePage {
  background: rgba(31, 41, 55, 0.9);
  border: 1px solid rgba(75, 85, 99, 0.3);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* 黑色模式下的表单标签 */
[data-theme="dark"] #userManagePage :deep(.ant-form-item-label > label) {
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框 */
[data-theme="dark"] #userManagePage :deep(.ant-input) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

/* 黑色模式下的输入框悬停状态 */
[data-theme="dark"] #userManagePage :deep(.ant-input:hover) {
  border-color: rgba(14, 165, 233, 0.6) !important;
}

/* 黑色模式下的输入框聚焦状态 */
[data-theme="dark"] #userManagePage :deep(.ant-input:focus),
[data-theme="dark"] #userManagePage :deep(.ant-input-focused) {
  border-color: #0ea5e9 !important;
  box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.2) !important;
}

/* 黑色模式下的占位符文字 */
[data-theme="dark"] #userManagePage :deep(.ant-input::placeholder) {
  color: #9ca3af !important;
}

/* 黑色模式下的表格 */
[data-theme="dark"] #userManagePage :deep(.ant-table) {
  background: rgba(31, 41, 55, 0.8) !important;
  color: #e5e7eb;
}

[data-theme="dark"] #userManagePage :deep(.ant-table-thead > tr > th) {
  background: rgba(17, 24, 39, 0.8) !important;
  color: #f3f4f6 !important;
  border-color: rgba(75, 85, 99, 0.3) !important;
}

[data-theme="dark"] #userManagePage :deep(.ant-table-tbody > tr > td) {
  background: rgba(31, 41, 55, 0.6) !important;
  color: #e5e7eb !important;
  border-color: rgba(75, 85, 99, 0.3) !important;
}

[data-theme="dark"] #userManagePage :deep(.ant-table-tbody > tr:hover > td) {
  background: rgba(55, 65, 81, 0.8) !important;
}

/* 黑色模式下的分页组件 */
[data-theme="dark"] #userManagePage :deep(.ant-pagination) {
  color: #e5e7eb;
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-item) {
  background: rgba(55, 65, 81, 0.8);
  border-color: rgba(75, 85, 99, 0.5);
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-item a) {
  color: #e5e7eb;
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-item:hover) {
  border-color: rgba(14, 165, 233, 0.6);
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-item-active) {
  background: rgba(14, 165, 233, 0.2);
  border-color: #0ea5e9;
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-item-active a) {
  color: #0ea5e9;
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-prev),
[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-next) {
  color: #e5e7eb;
}

[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-prev:hover),
[data-theme="dark"] #userManagePage :deep(.ant-pagination .ant-pagination-next:hover) {
  color: #0ea5e9;
}

/* 黑色模式下的按钮 */
[data-theme="dark"] #userManagePage :deep(.ant-btn:not(.ant-btn-primary)) {
  background: rgba(55, 65, 81, 0.8) !important;
  border-color: rgba(75, 85, 99, 0.5) !important;
  color: #e5e7eb !important;
}

[data-theme="dark"] #userManagePage :deep(.ant-btn:not(.ant-btn-primary):hover) {
  background: rgba(75, 85, 99, 0.8) !important;
  border-color: rgba(14, 165, 233, 0.6) !important;
  color: #f3f4f6 !important;
}

[data-theme="dark"] #userManagePage :deep(.ant-btn-dangerous) {
  background: rgba(239, 68, 68, 0.2) !important;
  border-color: rgba(239, 68, 68, 0.5) !important;
  color: #fca5a5 !important;
}

[data-theme="dark"] #userManagePage :deep(.ant-btn-dangerous:hover) {
  background: rgba(239, 68, 68, 0.3) !important;
  border-color: rgba(239, 68, 68, 0.7) !important;
  color: #f87171 !important;
}

/* 用户管理卡片式布局样式 */
.user-management-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.user-card {
  display: flex;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  min-height: 140px;
}

.user-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.15);
}

/* 黑色模式下的用户卡片 */
[data-theme="dark"] .user-card {
  background: rgba(31, 41, 55, 0.95);
  border: 1px solid rgba(75, 85, 99, 0.3);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

[data-theme="dark"] .user-card:hover {
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

/* 用户头像区域 */
.user-avatar {
  position: relative;
  flex-shrink: 0;
  width: 120px;
  height: 140px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: center;
}

[data-theme="dark"] .user-avatar {
  background: rgba(0, 0, 0, 0.2);
}

.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.user-card:hover .avatar-image {
  transform: scale(1.05);
}

.user-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  color: white;
  padding: 0.5rem;
  font-size: 0.75rem;
  line-height: 1.2;
}

.user-id {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.user-tags {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-end;
}

.user-tags .ant-tag {
  margin: 0;
  font-size: 0.7rem;
}

/* 用户信息区域 */
.user-info {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.info-section {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  padding-bottom: 0.75rem;
}

[data-theme="dark"] .info-section {
  border-bottom-color: rgba(255, 255, 255, 0.1);
}

.user-name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.25rem 0;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

[data-theme="dark"] .user-name {
  color: #e5e7eb;
}

.user-account {
  font-size: 0.875rem;
  color: #6b7280;
  margin: 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

[data-theme="dark"] .user-account {
  color: #9ca3af;
}

/* 详细信息 */
.info-details {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
}

.info-row {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.info-item.full-width {
  flex: 1 1 100%;
}

.info-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: #6b7280;
  white-space: nowrap;
  flex-shrink: 0;
}

[data-theme="dark"] .info-label {
  color: #9ca3af;
}

.info-value {
  font-size: 0.875rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.4;
}

[data-theme="dark"] .info-value {
  color: #d1d5db;
}

/* 操作按钮区域 */
.user-actions {
  display: flex;
  align-items: flex-end;
  padding: 1rem 1rem 1rem 0;
}

.user-actions .ant-btn {
  font-size: 0.75rem;
  padding: 4px 12px;
  height: 28px;
}

/* 分页容器 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 1.5rem 0;
  background: rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

[data-theme="dark"] .pagination-container {
  background: rgba(31, 41, 55, 0.8);
  border: 1px solid rgba(75, 85, 99, 0.3);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .user-management-list {
    grid-template-columns: repeat(auto-fill, minmax(450px, 1fr));
  }
}

@media (max-width: 768px) {
  .user-management-list {
    grid-template-columns: 1fr;
  }
  
  .user-card {
    flex-direction: column;
    min-height: auto;
  }
  
  .user-avatar {
    width: 100%;
    height: 120px;
  }
  
  .user-actions {
    padding: 1rem;
    justify-content: center;
  }
}
</style>
